<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="房号" prop="roomNum">
        <el-input
          v-model="queryParams.roomNum"
          placeholder="请输入房号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>


      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="房号" align="center" prop="roomNum"/>
      <el-table-column label="手机号" align="center" prop="photo"/>
      <el-table-column label="状态" align="center" prop="state" :formatter="formatState"/>
      <el-table-column label="总价" align="center" prop="totalPrice"/>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="view(scope.row)"
          >详情
          </el-button>
          <el-button
            size="small"
            type="text"
            icon="el-icon-edit"
            :disabled="scope.row.state ==='1'"
            @click="submitForm(scope.row)"
          >送达
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-table v-loading="loading" :data="detailList" >
        <el-table-column label="菜品名称" align="center" prop="dishesName" />
        <el-table-column label="数量" align="center" prop="dishNum" />
        <el-table-column label="单价" align="center" prop="price" />
        <el-table-column label="总价" align="center" prop="totalPrice" />
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listOrder, getOrder, delOrder, addOrder, updateOrder } from '@/api/hotel/dishOrder'
  import { listDetail, getDetail, delDetail, addDetail, updateDetail } from "@/api/hotel/detail";

  export default {
    name: 'Order',
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 表格数据
        orderList: [],
        // 弹出层标题
        title: '',
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          roomNum: null,
          state: null,
          totalPrice: null,
          photo: null,
          detailId: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {},
        query: {
          orderId: null,
        },
      }
    },
    created() {
      this.getList ()
    },
    methods: {
      view(rowData){
        this.open = true
        this.query.orderId = rowData.id
          listDetail(this.query).then(response => {
            this.detailList = response.rows;
          });
      },
      /** 查询列表 */
      getList() {
        this.loading = true
        listOrder (this.queryParams).then (response => {
          this.orderList = response.rows
          this.total = response.total
          this.loading = false
        })
      },
      formatState(rowDate) {
        if(rowDate.state==='0'){
          return '未送达'
        }else{
          return '已送达'
        }
      },
      // 取消按钮
      cancel() {
        this.open = false
        this.reset ()
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          roomNum: null,
          state: null,
          totalPrice: null,
          photo: null,
          detailId: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          remark: null
        }
        this.resetForm ('form')
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1
        this.getList ()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm ('queryForm')
        this.handleQuery ()
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map (item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset ()
        this.open = true
        this.title = '添加'
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset ()
        const id = row.id || this.ids
        getOrder (id).then (response => {
          this.form = response.data
          this.open = true
          this.title = '修改'
        })
      },
      /** 提交按钮 */
      submitForm(rowData) {
        rowData.state = '1'
              updateOrder (rowData).then (response => {
                this.$modal.msgSuccess ('送达成功')
                this.getList ()
              })
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids
        this.$modal.confirm ('是否确认删除编号为"' + ids + '"的数据项？').then (function() {
          return delOrder (ids)
        }).then (() => {
          this.getList ()
          this.$modal.msgSuccess ('删除成功')
        }).catch (() => {
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download ('hotel/order/export', {
          ... this.queryParams
        }, `order_${new Date ().getTime ()}.xlsx`)
      }
    }
  }
</script>
